package com.wlyuan.open.scanner.domain.template;

import com.wlyuan.open.scanner.dao.entity.PriceDocIndexDO;
import com.wlyuan.open.scanner.domain.quotation.QuotationDown;
import com.wlyuan.open.scanner.domain.quotation.QuotationUp;
import com.wlyuan.open.scanner.utils.JsonUtils;
import com.wlyuan.open.scanner.utils.enums.quotation.TemplateMapping;
import com.wlyuan.open.scanner.utils.mappings.BaseMapping;
import org.mapstruct.*;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * @author lijing
 * @description
 */
public class TemplateConvert {

    private static final Convert CONVERT = Mappers.getMapper(Convert.class);

    @Mapper(uses = {TemplateMapping.class})
    public interface Convert {

        @Mappings({
                @Mapping(target = "id", source = "source.indexId"),
                @Mapping(target = "sourceKey", source = "source.indexId"),
                @Mapping(target = "createTime", source = "source.indexCreatetime"),
                @Mapping(target = "tenantId", source = "source.indexCreatorcompanyid"),

                @Mapping(target = "code", source = "source.indexCode"),
                @Mapping(target = "pactCode", source = "source.indexPactcode"),
                @Mapping(target = "name", source = "source.indexName"),
                @Mapping(target = "mode", source = "source.indexContracttype"),
                @Mapping(target = "startTime", source = "source.indexStarttime"),
                @Mapping(target = "endTime", source = "source.indexEndtime"),
                @Mapping(target = "remarks", source = "source.indexComments"),
                @Mapping(target = "createBy", source = "source.indexCreatorid"),
                @Mapping(target = "updateBy", source = "source.indexCreatorid"),
                @Mapping(target = "updateTime", source = "source.indexStatustime"),
                @Mapping(target = "agreeBy", source = "source.indexConfirmer"),
                @Mapping(target = "agreeAt", source = "source.indexConfirmtime"),
                @Mapping(target = "status", source = "source.indexStatus"),
                @Mapping(target = "customerTenantId", source = "source.indexCustomercompanyid"),

                @Mapping(target = "minimumFee", source = "spec.minimumFee"),
                @Mapping(target = "insuranceRate", source = "spec.insuranceRate"),
                @Mapping(target = "taxRate", source = "spec.taxRate"),
                @Mapping(target = "rules", source = "spec")

        })
        @BaseMapping
        Template convert(PriceDocIndexDO source, TemplateSpec spec);

        @AfterMapping
        default void after(@MappingTarget Template target, PriceDocIndexDO source) {
            target.setRaw(JsonUtils.withNullJson(source));
        }

        List<Template> convert(List<PriceDocIndexDO> list);

        @Mappings({
                @Mapping(target = "tenantId", source = "upTenantId"),
                @Mapping(target = "carrierId", source = "indexSupplierid"),
        })
        QuotationUp upConvert(PriceDocIndexDO source);

        @Mappings({
                @Mapping(target = "customerId", source = "indexCustomerid"),
                @Mapping(target = "tenantId", source = "downTenantId"),
        })
        QuotationDown downConvert(PriceDocIndexDO source);

    }

    public static List<Template> convert(List<PriceDocIndexDO> creation) {
        return CONVERT.convert(creation);
    }

    public static Template convert(PriceDocIndexDO source, TemplateSpec spec) {
        return CONVERT.convert(source, spec);
    }

    public static QuotationUp upConvert(PriceDocIndexDO source) {
        return CONVERT.upConvert(source);
    }

    public static QuotationDown downConvert(PriceDocIndexDO source) {
        return CONVERT.downConvert(source);
    }
}
